<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{
    //
	protected $table='roles';
	protected $guarded=[];
//	获取所有的角色
	public function all_role($where){
		$res=$this->where(function ($query) use($where){
			$query->where(['company_id'=>$where['company_id']]);
			if($where['status'] !=null ){
				$query->where(['status'=>$where['status']]);
			}
			if($where['display_name'] !=null ){
				$query->where('display_name','like','%'.$where['display_name'].'%');
			}
		})->with('userinfo')->get();
		return  $res;
	}

//	获取用户信息
	public function userinfo(){
		return $this->belongsTo('App\Model\Users','user_id');
	}


	public function roles()
	{
		return $this->belongsToMany(Users::class, 'role_user',  'role_id','user_id', 'id', 'id');
	}

//	获取该角色所有的用户
	public function Role_user($role_id){
		$data=$this->where(['id'=>$role_id])->with('roles')->first();
		foreach($data['roles'] as $kk=>$vv){
			$newArr[]=$vv['id'];
		}
		return $newArr;
	}

//	已分配权限的角色
	public function all_role_configured($role_id){
		$data=$this->with(['permission_role'=>function($query){
			$query->with('permission');
		}])->where(['id'=>$role_id])->first();
		return $data;
	}

	public function permission_role(){
		return $this->hasMany('App\Model\Permission_role','role_id');
	}

}
